<?php
namespace App\Service\Admin;

use Illuminate\Cache\RedisTaggedCache;

class PatientService extends BaseService
{
    //患者

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUser);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $head = file_exists($v['head']) ? '/'.$v['head'] : '';
            $pat_list = [];
            $upOnes = $this->dbConn($this->dbPatient)
                ->where('del', 0)
                ->where('uid', $v['id'])
                ->orderBy('id', 'asc')
                ->get();
            if (count($upOnes)) {
                foreach ($upOnes as $vv) {
                    $vv = (array) $vv;
                    $pat_list[] = [
                        'id' => $vv['id'],
                        'title' => '【病历】'.$vv['title'],
                    ];
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'realname' => $v['realname'],
                'head' => $head,
                'pat_list' => $pat_list,
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbPatient)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $res['imgList'] = [];
        $imgOnes = $this->dbConn($this->dbPatImg)
            ->where('del', 0)
            ->where('up_id', $id)
            ->get();
        if (count($imgOnes)) {
            foreach ($imgOnes as $v) {
                if (file_exists($v['img'])) {
                    $res['imgList'][] = '/'.$v['img'];
                }
            }
        }
        return $this->toback(0, '', $res);
    }
}